<template>
	<view class="container">
		<view class="header">
			<view class="list">
				<view class="left">
					<view class="title">欢迎使用</view>
					<view class="small-title">防伪查询小程序</view>
				</view>

				<view class="right" @click="ToBusiness" v-if="JSON.stringify(business) == '{}'">
					<image src="/static/images/user-circle.png" />
					登录
				</view>

				<view class="right" @click="ToBusiness" v-else>
					<image :src="business.avatar_cdn" />
					{{ business.nickname }}
				</view>
			</view>

			<view class="validate-input">
				<u-input :placeholder="placeholderText" v-model="code">
					<!-- #ifndef H5 -->
					<template slot="suffix">
						<u-icon size="28" name="scan" @click="onScanCode"></u-icon>
					</template>
					<!-- #endif -->
				</u-input>
			</view>
		</view>

		<view class="validate">
			<u-button :customStyle="validateBtn" :plain="true" text="开始验证" @click="onValidate"></u-button>
		</view>
		<!-- 引入信息提示 -->
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// #ifndef H5    
			// ifndef H5 表示不会在H5端出现
			placeholderText: '请输入防伪码或者扫防伪码',
			// #endif
			// #ifdef H5
			// ifdef H5 表示会在H5端出现
			placeholderText: '请输入防伪码',
			// #endif
			validateBtn: {
				width: '100%',
				height: '100%',
				background: 'transparent',
				color: '#fff'
			},
			business: {},
			code: ''
		}
	},
	methods: {
		onScanCode() {
			//扫码事件
			uni.scanCode({
				success: (res) => {
					let path = res.path ? res.path : '';
					console.log(res);
					if (path) {
						this.$u.route(path);
					};
				}
			})
		},
		onValidate() {
			if (!this.code.trim()) {
				this.$refs.uToast.show({
					type: 'error',
					message: '请输入防伪码'
				});
				return false;
			}

			// 把对象转成数组
			if (Object.keys(this.business).length == 0) {
				this.$refs.uToast.show({
					type: 'error',
					message: '请先登录账号'
				});
				return false;
			}

			this.$u.route({
				url: 'pages/validate/info',
				params: { code: this.code }
			});
		},
		ToBusiness() {
			this.$u.route('pages/business/index');
		}
	},
	onShow() {
		this.business = uni.getStorageSync('business') ? uni.getStorageSync('business') : {};
	}

}
</script>

<style>
.header {
	height: 90vw;
	background: linear-gradient(to right, #33ccff 0%, #91cbf8 100%);
	border-bottom-left-radius: 10px;
	border-bottom-right-radius: 10px;
	padding-top: 10vh;
}

.header .list {
	color: #fff;
	display: flex;
	justify-content: space-between;
}

.header .list .left {
	margin-left: 20px;
}

.header .list .left .title {
	margin-bottom: 10px;
}

.header .list .right {
	display: flex;
	align-items: center;
	background-color: #fff;
	padding: 0 5px 0 10px;
	color: #469eff;
	font-size: 12px;
	border-top-left-radius: 20px;
	border-bottom-left-radius: 20px;
	height: 35px;
}

.header .list .right image {
	width: 20px;
	height: 20px;
	margin-right: 10px;
	border-radius: 50%;
}

.header .validate-input {
	width: 80%;
	background: #fff;
	border-radius: 10px;
	overflow: hidden;
	margin: 0 auto;
	margin-top: 20vw;
}

.validate {
	width: 100px;
	height: 100px;
	border-radius: 50%;
	overflow: hidden;
	background: #2879fc;
	margin: 0 auto;
	margin-top: -50px;
	box-shadow: 0px 0px 10px 10px #eee;
}
</style>